<?php
	include_once ("../../php/include_all.php");
?>

<div class="widget-header widget-header-flat">
			<h5><i class="icon-calendar"></i>Master Klasifikasi Urusan</H5>
		</div>
		
		<div class="position-relative" id="page-content">
			<a href="#window" data-toggle="modal"  class="btn btn-small btn-primary2 no-radius btn-tambah"><i class="icon-plus"></i>Tambah Data</a>
			&nbsp;<span id="load" class="spinner"></span>
		</div>
		
		<div id="window" class="modal hide fade" role="dialog" area-labelledby="myModalLabel" aria-hidden="true">
			<header class="modal-header">
				<a href="#" class="close dat-clear" data-dismiss="modal">x</a>
				<h3>Data Klasifikasi Urusan</h3>
			</header>
		
		
		<div class="modal-body">
		<form id="kla-urusan">
			<input type="hidden" name="act" value="save_kla_urusan">
			<input type="hidden" id="id_klasifikasi" name="id_klasifikasi" value="0">
			<table class="table-form">
				<tbody>
					<tr>
						<td>Nama Urusan</td>
						<td>:</td>
						<td>
							<select  name="id_urusan" id="id_urusan">
							<option value="0" id='id_klasi'> pilih salah satu </option>
								<?php
									$urusan = get_datas("SELECT * FROM mus_urusan");
									foreach ($urusan as $urusan) {
								?>		<option value="<?php echo $urusan['id_urusan']?>">
																	<?php echo $urusan['nama_urusan']?>
										</option>
								<?php	
									}
								?>
						</select>
						</td>
					</tr>
					<tr>
						 <td>Kode Klasifikasi</td>
                        <td>:</td>
						<td>
							<? $kk = $_GET['kode_klasifikasi']?>
							<input type="text" id="kode_klasifikasi" class="input-extra" name="kode_klasifikasi" readonly="" >
						</td>
					</tr>
					<tr>
						<td>Nama Klasifikasi</td>
                        <td>:</td>
						<td>
							<input type="text" id="nama_klasifikasi" class="input-extra" name="nama_klasifikasi" placeholder="Nama Klasifikasi">
						</td>
					</tr>
					<tr>
                        <td></td>
                        <td></td>
                        <td>
							<a class="btn btn-primary2 btn-small bt-simpan-kla"><div id="loader"></div><i class="icon-save"></i>&nbsp;Simpan</a><span id="result"></span>
							<span id="result"></span>
						</td>
					</tr>
				</tbody>
			
			</table>
		</form>
	</div>
</div>
		
		<div class="box-content">
		<table class="table table-bordered geo-table table-hover" >
			<thead>
				<tr>
					<th style="text-align:center;" width="5%;">No.</th>
					<th style="text-align:center;" width="15%;">Urusan</th>
					<th style="text-align:center;" width="15%;">Kode Klasifikasi</th>
					<th style="text-align:center;" width="25%;">Nama Klasifikasi</th>
					<th style="text-align:center; "width="20%;">Edit</th>
				</tr>
			</thead>
			<tbody id="tbody_kla_urusan">
				<?php
					$no = 1;
					$query = get_datas("SELECT id_klasifikasi,nama_urusan, kode_klasifikasi, nama_klasifikasi FROM mus_klasifikasi K, mus_urusan U WHERE K.id_urusan=U.id_urusan ORDER BY kode_klasifikasi");			
					//tampil data
					foreach ($query as $query){
				?>
				<tr><td style="text-align:center;"><?php echo $no?></td>
					<td style="text-align:center;"><?php echo $query['nama_urusan']?></td>
					<td style="text-align:center;"><?php echo $query['kode_klasifikasi']?></td>
					<td style="text-align:center;"><?php echo $query['nama_klasifikasi']?></td>
					<td style="text-align:center;">
						<a href = "#window" data-toggle="modal" class="btn btn-small btn-info edit" name="<?php echo $query['id_klasifikasi'];?>"><i class="icon-edit icon-white"></i>&nbsp; Ubah</a>
						<a class="btn btn-small btn-danger hapus" name="<?php echo $query['id_klasifikasi'];?>" ><i class="icon-trash icon-white"></i>&nbsp; Hapus</a></td>
				</tr>
			<?php	
				$no++;
			}?>
			</tbody>
		</table>
		</div>
<script>
var urls = 'admin/master_kla_urusan/kla_urusan_save.php';

$('#id_urusan').change(function(){
	source ="admin/master_kla_urusan/id.php?ids="+this.value;
	var coba=$.get(source);
	coba.done(function(data){
		document.getElementById('kode_klasifikasi').value=data;	
	})
});

$('.bt-simpan-kla').click(function(){
	var btn  = $(this);
	var load = $('#loader');
	var rslt = $('#result');
	load.addClass('icon-spinner icon-spin icon-2x white');
	//btn.removeClass('btn-primary').addClass('btn-info');
	
	var form = $('#kla-urusan');
	var data = form.serializeArray();
	var post = $.post(urls,data);
	post.done(function(res){
			var result = res.split('__');
			if(result.length==3){
				if(result[0]=='success'){
					 rslt.html('<font color="green">Data Tersimpan...</font>');
					setTimeout(function(){
					rslt.html('');
					},1500);
				
					var tbody = $('#tbody_kla_urusan');
					tbody.html(result[2]);
					init();
					$('#id_klasifikasi').val('0');
					document.getElementById('kode_klasifikasi').value='';				
					document.getElementById('nama_klasifikasi').value='';
					document.getElementById('id_klasi').selected=true;
					$('#window').modal('hide');
					
					
				}else{
					rslt.html('<font color="red">'+res+'</font>');
				}
			}else{
				rslt.html('<font color="red">'+res+'</font>');
			}
				load.removeClass();
		btn.removeClass('btn-success').addClass('btn-primary');
	});
});
	
$('.btn-tambah').click(function(){
	$('#id_klasifikasi').val('0');
	document.getElementById('kode_klasifikasi').value='';				
	document.getElementById('nama_klasifikasi').value='';
	document.getElementById('id_klasi').selected=true;
});	

function init(){
	$('.hapus').click(function(){
		var btn  = $(this);
		var pid  = this.name;
		var load = $(this);
		
		  load.html('<div class="icon-spinner icon-spin icon-2x white"></div>');
		bootbox.confirm("Anda akan menghapus klasifikasi urusan ini?", function(result) {
			if(result==true){
				if(pid!=0){
					var post = $.post(urls,{act:"del_kla_urusan",id_kla:pid});
					post.done(function(res){
					
					var hasil = res.split('__');
						if(hasil.length==2){
							if(hasil[0] == 'success'){
							//	rslt.html('<font color="green">tersimpan...</font>');
							//	setTimeout(function(){
							//		rslt.html('');
							//	},1500);
								
								var tbody = $('#tbody_kla_urusan');
								tbody.html(hasil[1]);
								init();
									
							}
						} 
					});
					load.html('<i class="icon-trash icon-white"></i>Hapus');
				}
			}else {
			load.html('<i class="icon-trash icon-white"></i>Hapus');
			}
		});
	});
	
	$('.edit').click(function(){
		var id_kla= this.name;
		var form = $('#kla-urusan');
		var post = $.post(urls,{act:'edit_urusan',id_kla:id_kla});
		post.done(function(res){
			var value = res.split('__');
			$('#id_klasifikasi').val(value[0]);
			form.find('select[name="id_urusan"]').val(value[1]);
			form.find('input[name="kode_klasifikasi"]').val(value[2]);
			form.find('input[name="nama_klasifikasi"]').val(value[3]);
			
		 //	document.getElementById('kla-urusan').reset();
		});
	});
}

/*
$('.dat-clear').click(function(){
	$('#id_klasifikasi').val(0);
	document.getElementById('kla-urusan').reset();
});
 */

init();
</script>